/**
 * @Project Name:hoofungson-elk-log
 * @Package Name:cn.hoofungson.framework.elk.log.converter.pattern
 * @Since JDK 1.8
 */

package cn.hoofungson.framework.elk.log.converter.pattern;

import ch.qos.logback.classic.spi.ILoggingEvent;
import cn.hoofungson.framework.elk.log.util.AppInfoUtils;
import co.elastic.apm.api.ElasticApm;
import co.elastic.apm.api.Transaction;
import org.apache.commons.lang3.StringUtils;
import org.apache.skywalking.apm.toolkit.log.logback.v1.x.LogbackPatternConverter;

/**
 * @Description: TraceIdPatternConverter
 * @Author 胡松 hoofungson@163.com
 * @Date 2020-03-25 22:51
 * @Version V1.0
 */
public class TraceIdPatternConverter extends LogbackPatternConverter {
    /**
     * The convert method is responsible for extracting data from the event and
     * storing it for later use by the write method.
     *
     * @param event
     */
    @Override
    public String convert(ILoggingEvent event) {
        if (StringUtils.equals("ElasticAPM", AppInfoUtils.getAppInfo().getSupportApm())) {
            Transaction transaction = ElasticApm.currentTransaction();
            return transaction.getTraceId();
        } else if (StringUtils.equals("SkyWalking", AppInfoUtils.getAppInfo().getSupportApm())) {
            return super.convert(event);
        } else {
            return StringUtils.EMPTY;
        }
    }
}
